package com.itheima.test3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
接受图片并保存


给 客户端响应
 */
public class ServerTest {

    public static void main(String[] args) throws IOException {
        //创建输出流  用来把 文件保存
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy\\meinv.jpg"));
        //创建 服务端对象
        ServerSocket ss = new ServerSocket(10001);

        //建立连接 获取 Socket 对象
        Socket accept = ss.accept();

        //获取输入流
        InputStream is = accept.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        //边读编写
        byte[] bytes=new byte[1024];
        int len;
        while ((len=bis.read(bytes))!=-1){

            bos.write(bytes,0,len);
        }

        // 给客户端写一个响应
        String s="上传成功";
        OutputStream os = accept.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
        writer.write(s);
       /* writer.flush();
        accept.shutdownOutput();*/
       writer.close();
        //释放资源

        ss.close();
        bos.close();


    }
}
